﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Text.RegularExpressions;
using TextMIS;

namespace TextMIS
{
    public partial class frmBranch : Form
    {
        public frmBranch()
        {
            InitializeComponent();
        }
        private void frmBranch_Load(object sender, EventArgs e)
        {
            //new Common.Common().AddImages(imageList1);
            Tr_BraList.ImageList = imageList1;

            DataBind();

        }

        dbopr BranchDB = new dbopr();
        string str_state = "Update";
        void DataBind()
        {
            Tr_BraList.Nodes.Clear();
            TreeNode node = new TreeNode("体检科室");
            DataTable dt = BranchDB.Get_Bran_list();
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode node1 = new TreeNode(dr["Bra_name"].ToString());
                node1.Tag = dr["Bra_id"].ToString();
                node.Nodes.Add(node1);
            }
            Tr_BraList.Nodes.Add(node);
            Tr_BraList.ExpandAll();
            //new Common.Common().AddImage(node);
        }

        private void bt_add_Click(object sender, EventArgs e)
        {   
            ClearControl();
            tb_id.Text = BranchDB.Get_MaxStr("BranchInfo", "Bra_id");
            str_state = "Insert";
        }
        void ClearControl()
        {
            tb_id.Text = "";
            tb_disp_order.Text = "";
            tb_name.Text = "";

            rb_jcjylx1.Checked = false;
            rb_jcjylx2.Checked = false;
            rb_jcjylx3.Checked = false;

            tb_BraInfo   .Text = "";
            tb_Brife_sum  .Text = "";
            tb_memo .Text = "";
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            if (tb_id.Text.Trim() == "") return;
            string str_jcjylx = "-1";
            if (rb_jcjylx1.Checked) str_jcjylx = "0";
            if (rb_jcjylx2.Checked) str_jcjylx = "1";
            if (rb_jcjylx3.Checked) str_jcjylx = "2";
            if (str_state == "Update")
            {
                BranchDB.Update_Bran(tb_id.Text.Trim(), tb_disp_order.Text.Trim(), tb_name.Text.Trim(), str_jcjylx, tb_Brife_sum.Text.Trim(), tb_BraInfo.Text.Trim(), tb_memo.Text.Trim());
                MessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DataBind();
            }
            if (str_state == "Insert")
            {
                BranchDB.Insert_Bran(tb_id.Text.Trim(), tb_disp_order.Text.Trim(), tb_name.Text.Trim(), str_jcjylx, tb_Brife_sum.Text.Trim(), tb_BraInfo.Text.Trim(), tb_memo.Text.Trim());
                MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                str_state = "Update";
                DataBind();
            }
        }


        

        private void bt_delete_Click(object sender, EventArgs e)
        {
            if (tb_id.Text.Trim() == "") return;
            //注意项目引用了，就不允许删除，暂时没有控制
            if (BranchDB.Exists_BraBy(tb_id.Text.Trim()) > 0)
            {
                MessageBox.Show("该项目被引用了，不允许删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            BranchDB.Delete_Bran(tb_id.Text.Trim());
            MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ClearControl();
            DataBind();
        }

        private void Bra_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (object.Equals(null, Tr_BraList.SelectedNode)) return;
            if (object.Equals(null, Tr_BraList.SelectedNode.Tag)) return;

            string str_id = Tr_BraList.SelectedNode.Tag.ToString().Trim();
            DataTable dt = BranchDB.Get_Bran_info(str_id);
            tb_id.Text = dt.Rows[0]["Bra_id"].ToString().Trim();
            tb_disp_order.Text = dt.Rows[0]["disp_order"].ToString().Trim();
            tb_name.Text = dt.Rows[0]["Bra_name"].ToString().Trim();

            if (dt.Rows[0]["Bra_type"].ToString().Trim() == "0")//实验室检查
                rb_jcjylx1.Checked = true;
            else if (dt.Rows[0]["Bra_type"].ToString() == "1")//物理检查
                rb_jcjylx2.Checked = true;
            else if (dt.Rows[0]["Bra_type"].ToString() == "2")//功能检查
                rb_jcjylx3.Checked = true;
            else
            {
                rb_jcjylx1.Checked = false;
                rb_jcjylx2.Checked = false;
                rb_jcjylx3.Checked = false;
            }
            tb_Brife_sum  .Text = dt.Rows[0]["Brife_sum"].ToString().Trim();
            tb_BraInfo.Text = dt.Rows[0]["Bra_info"].ToString().Trim();
            tb_memo.Text = dt.Rows[0]["Bra_memo"].ToString().Trim();
            str_state = "Update";
            
            
            //if (Tr_BraList.SelectedNode.Nodes.Count == 0)
            //{
            //    Tr_BraList.SelectedNode.SelectedImageIndex = Tr_BraList.SelectedNode.ImageIndex;
            //}


        }
        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


}
}
